﻿using System;
using System.ComponentModel;
using System.Windows;
using Esponce.Common;
using Esponce.QRCode;

namespace Esponce.Models
{
	public class TrackQRCodeViewModel : INotifyPropertyChanged
	{
		#region Constructor
		public TrackQRCodeViewModel()
		{
			this.QRCode = new TrackQRCode();

#if DEBUG
			if (Settings.IsDesignMode)
			{
				this.QRCode = new TrackQRCode()
				{
					Id = "101",
					Name = "Lorem Ipsum",
					Created = DateTime.UtcNow,
				};
			}
#endif
		}
		#endregion

		#region Public properties
		public Actions Action
		{
			get
			{
				return this.action;
			}
			set
			{
				if (this.action != value)
				{
					this.action = value;
					OnPropertyChanged("Action");
				}
			}
		}
		private Actions action;

		public string QRCodeId
		{
			get
			{
				return this.qrcodeId;
			}
			set
			{
				if (this.qrcodeId != value)
				{
					this.qrcodeId = value;
					OnPropertyChanged("QRCodeId");
				}
			}
		}
		private string qrcodeId;

		public TrackQRCode QRCode
		{
			get
			{
				return this.qrcode;
			}
			set
			{
				if (this.qrcode != value)
				{
					this.qrcode = value;
					OnPropertyChanged("QRCode");
				}
			}
		}
		private TrackQRCode qrcode;
		#endregion

		#region Public methods
		public void Select()
		{
			try
			{
				var client = App.ViewModel.CreateClient();
				App.Logger.WriteLine("GET " + client.UriBuilder.BuildTrackQRCodeUrl(this.QRCodeId));
				this.QRCode = client.GetTrackQRCode(this.QRCodeId);

				if (client.LastWarning != null && client.LastWarning.StartsWith("220 "))
				{
					MessageBox.Show(client.LastWarning, "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
				}

				App.Logger.WriteLine(string.Format("Completed! Rate limit: {0} requests remaining out of {1}", client.RateLimitRemaining, client.RateLimitTotal));
			}
			catch (Exception ex)
			{
				App.Logger.Log(ex);
			}
		}

		public void Insert()
		{
			try
			{
				var client = App.ViewModel.CreateClient();
				App.Logger.WriteLine("PUT " + client.UriBuilder.BuildTrackQRCodeUrl());

				this.QRCode.Id = null;
				this.QRCode.CampaignId = null;
				string id = client.CreateTrackQRCode(this.QRCode);

				//Update 'id' on the form
				this.QRCode.Id = id;
				this.QRCodeId = id;

				App.Logger.WriteLine(string.Format("Completed! Rate limit: {0} requests remaining out of {1}", client.RateLimitRemaining, client.RateLimitTotal));
			}
			catch (Exception ex)
			{
				App.Logger.Log(ex);
			}
		}

		public void Update()
		{
			try
			{
				var client = App.ViewModel.CreateClient();
				App.Logger.WriteLine("POST " + client.UriBuilder.BuildTrackQRCodeUrl(this.QRCodeId));
				client.UpdateTrackQRCode(this.QRCode);

				//if (client.LastWarning != null)
				//{
				//  MessageBox.Show(client.LastWarning, "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
				//}

				App.Logger.WriteLine(string.Format("Completed! Rate limit: {0} requests remaining out of {1}", client.RateLimitRemaining, client.RateLimitTotal));
			}
			catch (Exception ex)
			{
				App.Logger.Log(ex);
			}
		}

		public void Delete()
		{
			try
			{
				var client = App.ViewModel.CreateClient();
				App.Logger.WriteLine("DELETE " + client.UriBuilder.BuildTrackQRCodeUrl(this.QRCodeId));
				client.DeleteTrackQRCode(this.QRCodeId);

				App.Logger.WriteLine(string.Format("Completed! Rate limit: {0} requests remaining out of {1}", client.RateLimitRemaining, client.RateLimitTotal));
			}
			catch (Exception ex)
			{
				App.Logger.Log(ex);
			}
		}
		#endregion

		#region INotifyPropertyChanged Members
		public event PropertyChangedEventHandler PropertyChanged;
		protected virtual void OnPropertyChanged(string propertyName)
		{
			if (PropertyChanged != null)
			{
				PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
			}
		}
		#endregion
	}
}
